let houseId = "";
//页面加载时
$().ready(function(){
    //图片放大
    modelTost();
    houseId = getQueryString("house_id");
    console.log(houseId);

    $.ajax({
        url: "../house?house_id=" + houseId,    //请求的url地址
        dataType: "json",   //返回格式为json
        contentType: "application/json;charset=UTF-8",
        async: true,//请求是否异步，默认为异步，这也是ajax重要特性
        type: "GET",   //请求方式
        beforeSend:function(){
            //请求前的处理
            console.log("开始处理请求...");
        },
        success:function(req){
            ////将house对象中的值在detail页面展示
            showDetail(req);

        },
        complete:function(){
            //请求完成的处理
        },
        error:function(){
            //请求出错处理
            console.log("处理失败")
        }
    });
});

//文件上传
$('.btn-select-file').click(function () {
    $('#houseFile').click();
    console.log($('#houseFile').val());
});
//文件资源管理器点击时触发的事件
function setBlogrollImageName(basicfile){
    console.log(basicfile.value);
    $('.house-img-div').append("<span style='font-weight: bolder; margin: 10px'>New Image:</span style='font-weight: bolder'><u>" + basicfile.value + "</u>");
}

//点击修改按钮时
$("#house-update-btn").click(function(){
    let img = "";
    console.log(houseId);
    //获取house对象的json数据
    let house = getHouse();
    //重新上传图片
    if($("[name='house_image_file']").val() != "") {
        house.houseImage = $("[name='house_image_file']").val();
        let myForm = new FormData();
        myForm.append("img",  $("[name='house_image_file']")[0].files[0]);
        $.ajax({
            url: "../house/fileupload?house_id=" + houseId,    //请求的url地址
            type: "POST",
            data: myForm,
            async: false,
            contentType: false,
            processData: false,
            success:function(req){
                if (req.code == 200) {
                    img = req.data;
                }else {
                    alert(req.data);
                }
            },
        });
    }
    if(img != "") {
        house.houseImage = img;
    }
    let houseJson = JSON.stringify(house);
    console.log(houseJson);
    $.ajax({
        url:"../house?house_id=" + houseId,
        dataType: "json",   //返回格式为json
        contentType: "application/json;charset=UTF-8",
        async: true,//请求是否异步，默认为异步，这也是ajax重要特性
        type:"PUT",
        data: houseJson,
        dataType:"json",
        success:function(result){
            if (result.code != "200") {
                alert(result.data);
            }

        },
        error:function(){
            //请求出错处理
            console.log("处理失败")
        }
    })
});

//点击取消按钮
$('#house-cancel-btn').click(function () {
    window.location.replace("../html/house-release.html");
});
